/**
 * Created by hps on 17-4-30.
 */
public class LinkList {
    private Link first;

    public boolean isEmpty() {
        return first == null;
    }

    public void insertFirst(Link link) {
        link.next = first;
        first = link;
    }

    public Link deleteFirst() {
        Link temp = first;
        first = first.next;
        return temp;
    }

    public Link find(int key) {
        Link current = first;
        while (current.iData != key) {
            if(current.next == null)
                return null;
            current = current.next;
        }
        return current;
    }

    public Link delete(int key) {
        Link current = first;
        Link pre = first;
        while (current.iData != key) {
            if(current.next == null)
                return null;
            pre = current;
            current = current.next;
        }

        if(current == first)
            first = first.next;
        else
            pre.next = current.next;
        return current;
    }

    public void  displayList() {
        System.out.print("List (first-->last): ");
        Link current = first;
        while (current != null) {
            current.displayLink();
            current = current.next;
        }
        System.out.println();
    }
}
